﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Core {
    public static class Extensions {

        #region List 扩展
        /// <summary>
        /// 将List<string>转换成用逗号分隔开的字符串
        /// </summary>
        /// <param name="stringList"></param>
        /// <returns></returns>
        public static string ToHasSeparatorString(this List<string> stringList) {
            if (stringList == null) {
                return string.Empty;
            }
            StringBuilder result = new StringBuilder();
            bool flag = false;
            foreach (var s in stringList) {
                if (flag) {
                    result.Append(",");
                } else {
                    flag = true;
                }
                result.Append(s);
            }
            return result.ToString();
        }
        #endregion

        #region String 扩展
        /// <summary>
        /// 将传入的字符串中间部分字符替换成特殊字符
        /// </summary>
        /// <param name="value">需要替换的字符串</param>
        /// <param name="startLen">前保留长度</param>
        /// <param name="endLen">尾保留长度</param>
        /// <param name="replaceChar">特殊字符</param>
        /// <returns>被特殊字符替换的字符串</returns>
        public static string ReplaceWithSpecialChar(this string value, int startLen = 4, int endLen = 4, char specialChar = '*') {
            try {
                int lenth = value.Length - startLen - endLen;

                string replaceStr = value.Substring(startLen, lenth);

                string specialStr = string.Empty;

                for (int i = 0; i < replaceStr.Length; i++) {
                    specialStr += specialChar;
                }

                value = value.Replace(replaceStr, specialStr);
            }
            catch (Exception) {
                throw;
            }

            return value;
        }
        #endregion
    }
}
